package pp.gateway.adapter.user.login;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pp.gateway.adapter.user.vo.LogOutVO;
import pp.gateway.adapter.user.vo.LoginByAccountVO;
import pp.gateway.adapter.user.vo.ParseTokenVO;
import pp.gateway.adapter.user.vo.UserRegisterVO;
import pp.gateway.application.user.login.api.LoginService;
import pp.gateway.application.user.login.dto.LoginByAccountDTO;
import pp.gateway.application.user.login.dto.ParseTokenDTO;
import pp.infrastructure.http.Response;

@RestController
@RequestMapping("/pp/user/login")
@Slf4j
public class UserLoginController {

    @Autowired
    LoginService loginService;


    @PostMapping("/loginByAccount")
    public Response<LoginByAccountDTO> loginByAccount(@RequestBody LoginByAccountVO input) {
        try {
            log.info("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "用户登录；参数 input = " + JSON.toJSONString(input));
            LoginByAccountDTO dto = loginService.loginByAccount(input.toParam());
            return Response.success(dto, "登录成功");
        } catch (Exception e) {
            log.error("{}.{} --> {}", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "登录失败，参数 input = " + JSON.toJSONString(input), e);
            return Response.fail();

        }
    }

    @PostMapping("/register")
    public Response<Boolean> UserRegister(@RequestBody UserRegisterVO input) {
        try {
            log.info("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "用户注册；参数 input = " + JSON.toJSONString(input));
            loginService.UserRegister(input.toParam());
            return Response.success("注册成功");
        } catch (Exception e) {
            log.error("{}.{} --> {}", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "注册失败，参数 input = " + JSON.toJSONString(input), e);
            return Response.fail();
        }
    }

    @PostMapping("/parseToken")
    public Response<ParseTokenDTO>parseToken(@RequestBody ParseTokenVO input){
        try{
            log.info("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "用户退出登录；参数 input = " + JSON.toJSONString(input));
            ParseTokenDTO result = loginService.parseToken(input.toParam());
            return Response.success(result, "此 Token 正确且有效！");
        } catch (Exception e){
            log.error("{}.{} --> {}", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "退出登录失败，参数 input = " + JSON.toJSONString(input), e);
            return Response.fail();
        }
    }

    @PostMapping("/logOut")
    public Response<Boolean> logOut(@RequestBody LogOutVO input){
        try{
            log.info("{}.{} --> {};", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "用户退出登录；参数 input = " + JSON.toJSONString(input));
            Boolean result = loginService.logOut(input.toParam());
            return Response.success(result, "退出成功");
        } catch (Exception e){
            log.error("{}.{} --> {}", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                    , "退出登录失败，参数 input = " + JSON.toJSONString(input), e);
            return Response.fail();
        }
    }

}
